package com.ruoyi.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.domain.entity.*;
import com.ruoyi.project.domain.query.ProjectAccountingQuery;
import com.ruoyi.project.domain.query.ProjectFeeQuery;
import com.ruoyi.project.domain.query.ProjectSaleQuery;
import com.ruoyi.project.domain.query.ProjectStoreQuery;
import com.ruoyi.project.mapper.ProjectSaleMapper;
import com.ruoyi.project.mapper.ProjectStoreMapper;
import com.ruoyi.project.service.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class ProjectSaleServiceImpl extends ServiceImpl<ProjectSaleMapper, ProjectSale> implements IProjectSaleService {
    @Resource
    private ProjectSaleMapper projectSaleMapper;


    @Resource
    IProjectAccountingService accountingService;

    @Resource
    IProjectCostingService costingService;


    @Resource
    IProjectService projectService;

    @Resource
    IProjectGoodsService goodsService;

    @Resource
    IProjectFeeService feeService;




    @Override
    @Async("taskExecutor") // 指定使用的线程池
    public void syncData(List<ProjectSale> list) {
        this.saveBatch(list); //更新或添加
    }

    @Override
    public List<ProjectSale> getList(ProjectSaleQuery query) {
        LambdaQueryWrapper<ProjectSale> wrapper = new QueryWrapper<ProjectSale>().lambda()
                .eq(query.getGoodsId() != null, ProjectSale::getGoodsId, query.getGoodsId())
                .eq(query.getHousekeeperProject() != null, ProjectSale::getHousekeeperProject, query.getHousekeeperProject());
        return this.list(wrapper);
    }

    @Override
    public Boolean addInfo(ProjectSale project) {
        return this.save(project);
    }

    @Override
    public Boolean updateInfo(ProjectSale project) {
        return this.updateInfo(project);
    }

    @Override
    public Boolean deleteInfo(String id) {
        return this.removeById(id);
    }

    @Override
    public ProjectSale getInfoById(String id) {
        return this.getById(id);
    }

    @Override
    public void deleteAll() {
        projectSaleMapper.deleteAll();
    }

    @Override
    public TableDataInfo getGjpSaleList(ProjectSaleQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPageNum(), query.getPageSize());
        ProjectAccountingQuery accountingQuery = new ProjectAccountingQuery();
        accountingQuery.setProjectNo(query.getProjectNo());
        accountingQuery.setRefresh(query.getRefresh());
        Map totalCost = accountingService.getTotalCost(accountingQuery);
        Object saleList = totalCost.get("saleList");
        List<ProjectSale> list = (List<ProjectSale>) saleList;
        setSaleList(list);
        return new TableDataInfo(list, page.getTotal());
    }


    @Override
    public List<ProjectSale> getAllGjpList(ProjectSaleQuery query) {
        List<ProjectSale> allGjpSale = projectSaleMapper.getAllGjpSale(query.getHousekeeperProject());

        //添加其他收入
        ProjectFeeQuery projectFeeQuery = new ProjectFeeQuery();
        projectFeeQuery.setHousekeeperProject(query.getHousekeeperProject());
        List<ProjectFee> projectFeeList = feeService.getOtherFee(projectFeeQuery);

        for (ProjectFee projectFee : projectFeeList) {
            ProjectSale sale = new ProjectSale();
            sale.setTotal(projectFee.getTotal());
            //其他收入 看作毛利?
            sale.setProfit(projectFee.getTotal());
            sale.setName(projectFee.getRemark());
            sale.setStatus("1");
            sale.setSaleDate(projectFee.getSubmitDate());
            sale.setAmount(1);
            allGjpSale.add(sale);
        }


// 遍历结果集，设置 goodsAllName
        for (ProjectSale sale : allGjpSale) {
            //过滤掉其他收入的
            if (sale.getId() == null) {
                continue;
            }
            // 获取需要拼接的字段
            String name = sale.getName();
            String fullName = sale.getFullName();
            String modal = sale.getModal();
            String param = sale.getParam();

            // 拼接 goodsAllName，主键用 '-' 连接
            StringBuilder goodsAllNameBuilder = new StringBuilder();
            if (fullName != null && !fullName.isEmpty()) {
                goodsAllNameBuilder.append(fullName).append("-");
            }
            if (name != null && !name.isEmpty()) {
                goodsAllNameBuilder.append(name).append("-");
            }
            if (modal != null && !modal.isEmpty()) {
                goodsAllNameBuilder.append(modal).append("-");
            }
            if (param != null && !param.isEmpty()) {
                goodsAllNameBuilder.append(param);
            }

            // 移除末尾多余的 '-'
            String goodsAllName = goodsAllNameBuilder.toString();
            if (goodsAllName.endsWith("-")) {
                goodsAllName = goodsAllName.substring(0, goodsAllName.length() - 1);
            }

            // 设置拼接后的值到 goodsAllName
            sale.setGoodsAllName(goodsAllName);
        }
        return allGjpSale;
    }

    public void setSaleList(List<ProjectSale> list) {
        for (ProjectSale sale : list) {
            // 获取需要拼接的字段
            String name = sale.getName();
            String fullName = sale.getFullName();
            String modal = sale.getModal();
            String param = sale.getParam();

            // 拼接 goodsAllName，主键用 '-' 连接
            StringBuilder goodsAllNameBuilder = new StringBuilder();
            if (fullName != null && !fullName.isEmpty()) {
                goodsAllNameBuilder.append(fullName).append("-");
            }
            if (name != null && !name.isEmpty()) {
                goodsAllNameBuilder.append(name).append("-");
            }
            if (modal != null && !modal.isEmpty()) {
                goodsAllNameBuilder.append(modal).append("-");
            }
            if (param != null && !param.isEmpty()) {
                goodsAllNameBuilder.append(param);
            }

            // 移除末尾多余的 '-'
            String goodsAllName = goodsAllNameBuilder.toString();
            if (goodsAllName.endsWith("-")) {
                goodsAllName = goodsAllName.substring(0, goodsAllName.length() - 1);
            }

            // 设置拼接后的值到 goodsAllName
            sale.setGoodsAllName(goodsAllName);
        }
    }

}
